package com.fromfuture.xuhuidoc.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.customview.NumImageView;

import java.util.List;

/**
 * Author：何送军
 * creation time：2017/8/21
 **/
public class DocRecycleViewAdapter extends RecyclerView.Adapter <DocRecycleViewAdapter.MyViewHolder>{

    List<String> list;
    List<Integer>listnum;
    Context context;

    onItemClickListener mOnItemClickListener;
    onItemLongClickListener mOnItemLongClickListener;
    numChange numChange;

    public DocRecycleViewAdapter(List<String> list,List<Integer>listnum, Context context) {
        this.list = list;
        this.listnum=listnum;
        this.context = context;
    }

    /**
     * 在pos位置之前插入str
     *
     * @param pos
     * @param str
     */
    public void addStr(int pos, String str) {
        list.add(pos, str);
        //局部刷新
        notifyItemInserted(pos);
    }

    public void remove(int pos) {
        list.remove(pos);
        notifyItemRemoved(pos);
    }

    public void nuchangShican(int pos,int num){
        listnum.set(pos,num);

        notifyItemChanged(pos);
    }


    public void setmOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setmOnItemLongClickListener(onItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public void setNumChange(numChange numChange){
        this.numChange=numChange;
    }

    //创建itemView
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //构建item布局
        View view = LayoutInflater.from(context).inflate(R.layout.recycleview_doc_circle_item, parent, false);
        return new MyViewHolder(view);
    }

    //把数据绑定到ViewHolder的控件上
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.textView.setText(list.get(position));
        holder.imageView.setNum(listnum.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mOnItemLongClickListener.onItemLongClickListener(position);
                numChange.numChanges(position);
                return true;
            }
        });

    }

    //加载数据的大小
    @Override
    public int getItemCount() {
        return list.size();
    }

    //RecyclerView.ViewHolder实现了复用,但是是抽象类
    //查找控件
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        NumImageView imageView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.doc_recycle_view_txt);
            imageView=(NumImageView) itemView.findViewById(R.id.doc_recycle_view_img);
        }
    }


    public interface onItemClickListener {
        public void onItemClickListener(int pos);
    }

    public interface onItemLongClickListener {
        public void onItemLongClickListener(int pos);
    }
    public interface numChange{
        public void numChanges(int pos);
    }


}
